#include "Sprite.h"

#include "Game.h"

void CSprite::load(String sFilename)
{
    
    Texture texture = CGame::getInstance()->getTextureHandler()->getTexture(sFilename.c_str());
    m_iTexture = texture.iTexIndex;
    m_iWidth = texture.iWidth;
    m_iHeight = texture.iHeight;
    
    //TODO: Remove this hack, replace with proper file loading
    m_vCOMOffset = Vector(m_iWidth/2.f, m_iHeight/2.f);
    
}

void CSprite::render()
{
    
    //Colour the sprite
    glColor4f(m_rgbaColor.r, m_rgbaColor.g, m_rgbaColor.b, m_rgbaColor.a);
    
    glPushMatrix();
    
    //Center the sprite on the COM
    glTranslatef(-m_vCOMOffset[0], -m_vCOMOffset[1], 0);
    glScalef(m_iWidth, m_iHeight, 1.f);
    
    //Bind the sprite's texture
    glBindTexture(GL_TEXTURE_2D, m_iTexture);
    
    glBegin(GL_QUADS);
        
        glTexCoord2f(0.f, 0.f);
        glVertex3f(0.f, 0.f, 0.f);
        glTexCoord2f(1.f, 0.f);
        glVertex3f(1.f, 0.f, 0.f);
        glTexCoord2f(1.f, 1.f);
        glVertex3f(1.f, 1.f, 0.f);
        glTexCoord2f(0.f, 1.f);
        glVertex3f(0.f, 1.f, 0.f);
        
    glEnd();
    
    glPopMatrix();
    
}
